package com.ruoyi.web.controller.gcgl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ConvertUpMoneyUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gcgl.domain.GcClbxd;
import com.ruoyi.gcgl.domain.GcClbxditem;
import com.ruoyi.gcgl.domain.GcProject;
import com.ruoyi.gcgl.domain.SjProcessConfig;
import com.ruoyi.gcgl.domain.SjProjectPeople;
import com.ruoyi.gcgl.service.IGcClbxdService;
import com.ruoyi.gcgl.service.IGcClbxditemService;
import com.ruoyi.gcgl.service.IGcCustomerService;
import com.ruoyi.gcgl.service.IGcProjectService;
import com.ruoyi.gcgl.service.IGcSendTipService;
import com.ruoyi.gcgl.service.ISjProcessConfigService;
import com.ruoyi.gcgl.service.ISjProjectPeopleService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysRoleService;

/**
 * 差旅核销单Controller
 * 
 * @author ruoyi
 * @date 2022-12-17
 */
@Controller
@RequestMapping("/gcgl/clbxd")
public class GcClbxdController extends BaseController
{
    private String prefix = "gcgl/clbxd";

    @Autowired
    private IGcClbxdService gcClbxdService;
    @Autowired
    private IGcClbxditemService gcClbxditemService;
    @Autowired
    private IGcSendTipService gcSendTipService;
    @Autowired
    private IGcProjectService gcProjectService;
    @Autowired
    private IGcCustomerService gcCustomerService;
    @Resource
    private RuntimeService runtimeService;
    @Resource
    private TaskService taskService;
    @Autowired
    private ISjProcessConfigService sjProcessConfigService;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private ISysRoleService roleService;
    
    final String processCode = "sjbaoxiao";
    @Autowired
    private ISjProjectPeopleService sjProjectPeopleService;

    @RequiresPermissions("gcgl:clbxd:view")
    @GetMapping()
    public String clbxd()
    {
        return prefix + "/clbxd";
    }

    /**
     * 查询差旅核销单列表
     */
    @RequiresPermissions("gcgl:clbxd:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GcClbxd gcClbxd)
    {
        startPage();
        gcClbxd.setDeleteFlag(1);
      //角色数据权限 ==根据用户定义的数据权限查看项目数据
        List<SysRole> roles = getSysUser().getRoles();
        List<Long> roleIds = new ArrayList<Long>();
        for(SysRole obj:roles){
        	roleIds.add(obj.getRoleId());
        }
        
        int count = roleService.selectCountRoleDeptByRoleId(roleIds);
        if(count != 0){
        	//看部门
        	gcClbxd.setRoleIds(roleIds);
        }else{
        	//看个人
        	if(getSysUser().isShowRoleIds(roleIds)){
        		gcClbxd.setOwnerId(getUserId());
            }
        }
        //----
        List<GcClbxd> list = gcClbxdService.selectGcClbxdList(gcClbxd);
        return getDataTable(list);
    }

    /**
     * 导出差旅核销单列表
     */
    @RequiresPermissions("gcgl:clbxd:export")
    @Log(title = "差旅核销单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GcClbxd gcClbxd)
    {
    	gcClbxd.setDeleteFlag(1);
    	if(getSysUser().isShow()){
            gcClbxd.setCreateUser(getLoginName());
            }
        List<GcClbxd> list = gcClbxdService.selectGcClbxdList(gcClbxd);
        ExcelUtil<GcClbxd> util = new ExcelUtil<GcClbxd>(GcClbxd.class);
        return util.exportExcel(list, "差旅核销单数据");
    }
    
    /**
     * 新增差旅核销单
     */
    @GetMapping("/add")
    public String add(ModelMap mmap){
    	mmap.put("chucr", getLoginName());
    	mmap.put("chucsj", DateUtils.getDate());
    	
    	mmap.put("owner", getLoginName());
    	mmap.put("ownerId", getUserId());
    	mmap.put("deptName", getSysUser().getDept().getDeptName());
    	mmap.put("deptId", getSysUser().getDept().getDeptId());
    	
    	//下一级审核人
        List<SjProcessConfig> configlis = returnUser(getSysUser().getDept().getDeptId());
        mmap.put("approveid", configlis.get(0).getLevel1id());
        mmap.put("approvename", configlis.get(0).getLevel1name());
        
        //所属
        SysDept dept = new SysDept();
        dept.setDelFlag("1");
        dept.setParentId(100l);
        List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
        mmap.put("deptlis", deptlis);
        return prefix + "/add4empty";
    }

    /**
     * 新增差旅核销单
     */
    @GetMapping("/add/{id}")
    public String add(ModelMap mmap,@PathVariable("id") Long id){
    	mmap.put("chucr", getLoginName());
    	mmap.put("chucsj", DateUtils.getDate());
    	
    	GcProject po = gcProjectService.selectGcProjectById(id);
    	mmap.put("gcProject", po);
    	
    	mmap.put("owner", getLoginName());
    	mmap.put("ownerId", getUserId());
    	mmap.put("deptName", getSysUser().getDept().getDeptName());
    	mmap.put("deptId", getSysUser().getDept().getDeptId());
    	
    	//下一级审核人
        List<SjProcessConfig> configlis = returnUser(getSysUser().getDept().getDeptId());
        mmap.put("approveid", configlis.get(0).getLevel1id());
        mmap.put("approvename", configlis.get(0).getLevel1name());
        
      //所属
        SysDept dept = new SysDept();
        dept.setDelFlag("1");
        dept.setParentId(100l);
        List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
        mmap.put("deptlis", deptlis);
        return prefix + "/add";
    }

    /**
     * 新增保存差旅核销单
     */
    @RequiresPermissions("gcgl:clbxd:add")
    @Log(title = "差旅核销单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public synchronized AjaxResult addSave(GcClbxd gcClbxd)
    {
    	try {
    		SimpleDateFormat sdy = new SimpleDateFormat("yyyy");
    		SimpleDateFormat sdm = new SimpleDateFormat("MM");
    		int year = Integer.parseInt(sdy.format(new Date()));
    		int month = Integer.parseInt(sdm.format(new Date()));
    		GcClbxd newObj = new GcClbxd();
    		newObj.setDeleteFlag(1);
    		newObj.setCreateAt(DateUtils.getFisrtDayOfMonth(year, month));
    		newObj.setUpdateAt(DateUtils.getLastDayOfMonth(year, month));
    		List<GcClbxd> lisCount = gcClbxdService.selectGcClbxdList(newObj);
    		String no = String.format("%02d", lisCount.size()+1);
    		String l_month = String.format("%02d", month);
    		gcClbxd.setOrderNo(year+"-"+l_month+"-"+no);
    		gcClbxd.setDeleteFlag(1);
    		gcClbxd.setCreateAt(new Date());
    		gcClbxd.setUpdateAt(new Date());
    		gcClbxd.setCreateUser(getLoginName());
    		gcClbxd.setUpdateUser(getLoginName());
    		gcClbxd.setStatus(1);
    		gcClbxd.setBxr(getLoginName());
    		gcClbxd.setBxrAt(new Date());
    		gcClbxdService.insertGcClbxd(gcClbxd);
    		
    		Double hejexx = 0.0;
        	for(GcClbxditem obj:gcClbxd.getItem()){
        		
        		double jtje = Double.parseDouble(obj.getJtje()==null?"0":obj.getJtje().toString());
//        		double ccbzbz = Double.parseDouble(obj.getCcbzbz()==null?"0":obj.getCcbzbz().toString());
        		double ccbzje = Double.parseDouble(obj.getCcbzje()==null?"0":obj.getCcbzje().toString());
        		double ccbzzs = Double.parseDouble(obj.getCcbzzs()==null?"0":obj.getCcbzzs().toString());
        		double qtfyjesnjt = Double.parseDouble(obj.getQtfyjesnjt()==null?"0":obj.getQtfyjesnjt().toString());
        		double qtfyjejery = Double.parseDouble(obj.getQtfyjejery()==null?"0":obj.getQtfyjejery().toString());
        		double qtfyjetcf = Double.parseDouble(obj.getQtfyjetcf()==null?"0":obj.getQtfyjetcf().toString());

        		hejexx+=jtje+ccbzje+ccbzzs+qtfyjesnjt+qtfyjejery+qtfyjetcf;
        		obj.setJine(Double.valueOf(hejexx));
        		
        		obj.setDeleteFlag(1);
        		obj.setCreateAt(new Date());
        		obj.setUpdateAt(new Date());
        		obj.setCreateUser(getLoginName());
        		obj.setUpdateUser(getLoginName());
        		obj.setClbxdId(gcClbxd.getId());
        		
        		gcClbxditemService.insertGcClbxditem(obj);
        	}
        	
        	gcClbxd.setBxrje(Double.valueOf(hejexx));
        	gcClbxd.setHejexx(hejexx.toString());
        	gcClbxd.setHejedx(ConvertUpMoneyUtil.toChinese(hejexx.toString()));
        	gcClbxd.setDjsl(gcClbxd.getItem().size()+"");
        	gcClbxdService.updateGcClbxd(gcClbxd);
        	
        	//发送消息通知
        	gcSendTipService.sendTip("clbx", gcClbxd.getType(), gcClbxd.getOrderNo(), 
        			gcClbxd.getCreateUser(), DateUtils.dateTime(gcClbxd.getCreateAt()), 
        			gcClbxd.getStatus().toString(), null, gcClbxd.getId().toString());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return toAjax(0);
		}
    	
        return toAjax(1);
    }

    /**
     * 修改差旅核销单
     */
    @RequiresPermissions("gcgl:clbxd:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        GcClbxd gcClbxd = gcClbxdService.selectGcClbxdById(id);
        GcClbxditem gcClbxditem = new GcClbxditem();
        gcClbxditem.setClbxdId(id);
        List<GcClbxditem> items = gcClbxditemService.selectGcClbxditemList(gcClbxditem);
        
        for(GcClbxditem item:items){
        	item.setCfrq(DateUtils.addDays(item.getCfrq(), 1));
        	item.setDdrq(DateUtils.addDays(item.getDdrq(), 1));
        }
        
        gcClbxd.setItem(items);
        mmap.put("gcClbxd", gcClbxd);
        
        //下一级审核人
        List<SjProcessConfig> configlis = returnUser(getSysUser().getDept().getDeptId());
        mmap.put("approveid", configlis.get(0).getLevel1id());
        mmap.put("approvename", configlis.get(0).getLevel1name());
        
      //所属
        SysDept dept = new SysDept();
        dept.setDelFlag("1");
        dept.setParentId(100l);
        List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
        mmap.put("deptlis", deptlis);
        return prefix + "/edit";
    }

    /**
     * 修改保存差旅核销单
     */
    @RequiresPermissions("gcgl:clbxd:edit")
    @Log(title = "差旅核销单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GcClbxd gcClbxd)
    {
    	try {
    		gcClbxditemService.deleteGcClbxditemById(gcClbxd.getId());
        	//item
    		Double hejexx = 0.0;
        	for(GcClbxditem item : gcClbxd.getItem()){
        		double jtje = Double.parseDouble(item.getJtje()==null?"0":item.getJtje().toString());
//        		double ccbzbz = Double.parseDouble(item.getCcbzbz()==null?"0":item.getCcbzbz().toString());
        		double ccbzje = Double.parseDouble(item.getCcbzje()==null?"0":item.getCcbzje().toString());
        		double ccbzzs = Double.parseDouble(item.getCcbzzs()==null?"0":item.getCcbzzs().toString());
        		double qtfyjesnjt = Double.parseDouble(item.getQtfyjesnjt()==null?"0":item.getQtfyjesnjt().toString());
        		double qtfyjejery = Double.parseDouble(item.getQtfyjejery()==null?"0":item.getQtfyjejery().toString());
        		double qtfyjetcf = Double.parseDouble(item.getQtfyjetcf()==null?"0":item.getQtfyjetcf().toString());

        		hejexx+=jtje+ccbzje+ccbzzs+qtfyjesnjt+qtfyjejery+qtfyjetcf;
        		item.setJine(Double.valueOf(hejexx));
        		
        		item.setDeleteFlag(1);
        		item.setUpdateAt(new Date());
        		item.setCreateUser(getLoginName());
        		item.setUpdateUser(getLoginName());
        		item.setClbxdId(gcClbxd.getId());
        		gcClbxditemService.insertGcClbxditem(item);        		
        	}
        	
        	gcClbxd.setBxrje(Double.valueOf(hejexx));
        	gcClbxd.setHejexx(hejexx.toString());
        	gcClbxd.setHejedx(ConvertUpMoneyUtil.toChinese(hejexx.toString()));
        	gcClbxd.setDjsl(gcClbxd.getItem().size()+"");
        	gcClbxd.setUpdateAt(new Date());
        	gcClbxd.setUpdateUser(getLoginName());
        	gcClbxdService.updateGcClbxd(gcClbxd);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return toAjax(0);
		}
    	
        return toAjax(1);
    }

    /**
     * 删除差旅核销单
     */
    @RequiresPermissions("gcgl:clbxd:remove")
    @Log(title = "差旅核销单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gcClbxdService.deleteGcClbxdByIds(ids));
    }
    
    /////财务审批
    @RequiresPermissions("gcgl:clbxd:cwpsview")
    @GetMapping("/cwps")
    public String cwps()
    {
        return prefix + "/clbxdcwps";
    }
    
    @PostMapping("/cwpslist")
    @ResponseBody
    public TableDataInfo cwpslist(GcClbxd gcClbxd)
    {
        startPage();
        gcClbxd.setDeleteFlag(1);
        gcClbxd.setStatus(0);
        List<GcClbxd> list = gcClbxdService.selectGcClbxdList(gcClbxd);
        return getDataTable(list);
    }
    
    @Log(title = "通过", businessType = BusinessType.UPDATE)
    @PostMapping("/cwyes")
    @ResponseBody
    public AjaxResult cwyes(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcClbxd obj = new GcClbxd();
    		obj.setId(Long.parseLong(id[i]));
    		obj.setStatus(1);
    		obj.setSpr(getLoginName());
    		obj.setSprAt(new Date());
    		gcClbxdService.updateGcClbxd(obj);    	
    		
    		GcClbxd gcClbxd = gcClbxdService.selectGcClbxdById(Long.parseLong(id[i]));
    		//发送消息通知
        	gcSendTipService.sendTip("clbx", gcClbxd.getType(), gcClbxd.getOrderNo(), 
        			gcClbxd.getCreateUser(), DateUtils.dateTime(gcClbxd.getCreateAt()), 
        			gcClbxd.getStatus().toString(), null, gcClbxd.getId().toString());
    	}
    	
    	return toAjax(1);
    }
    
    @Log(title = "不通过", businessType = BusinessType.UPDATE)
    @PostMapping("/cwno")
    @ResponseBody
    public AjaxResult cwno(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcClbxd obj = new GcClbxd();
    		obj.setId(Long.parseLong(id[i]));
    		obj.setStatus(2);
    		obj.setSpr(getLoginName());
    		obj.setSprAt(new Date());
    		gcClbxdService.updateGcClbxd(obj); 
    		
    		GcClbxd gcClbxd = gcClbxdService.selectGcClbxdById(Long.parseLong(id[i]));
    		//发送消息通知
        	gcSendTipService.sendTip("clbx", gcClbxd.getType(), gcClbxd.getOrderNo(), 
        			gcClbxd.getCreateUser(), DateUtils.dateTime(gcClbxd.getCreateAt()), 
        			gcClbxd.getStatus().toString(), null, gcClbxd.getId().toString());
    	}
    	return toAjax(1);
    }
    
    /////总经理审批
    @RequiresPermissions("gcgl:clbxd:zjlpsview")
    @GetMapping("/zjlps")
    public String zjlps()
    {
        return prefix + "/clbxdzjlps";
    }
    
    @PostMapping("/zjlpslist")
    @ResponseBody
    public TableDataInfo zjlpslist(GcClbxd gcClbxd)
    {
        startPage();
        gcClbxd.setDeleteFlag(1);
        gcClbxd.setStatus(1);
        List<GcClbxd> list = gcClbxdService.selectGcClbxdList(gcClbxd);
        return getDataTable(list);
    }
    
    @Log(title = "通过", businessType = BusinessType.UPDATE)
    @PostMapping("/zjlyes")
    @ResponseBody
    public AjaxResult zjlyes(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcClbxd obj = new GcClbxd();
    		obj.setId(Long.parseLong(id[i]));
    		obj.setStatus(3);
    		obj.setLdsp(getLoginName());
    		obj.setLdspAt(new Date());
    		gcClbxdService.updateGcClbxd(obj); 
    		
    		GcClbxd gcClbxd = gcClbxdService.selectGcClbxdById(Long.parseLong(id[i]));
    		//发送消息通知
        	gcSendTipService.sendTip("clbx", gcClbxd.getType(), gcClbxd.getOrderNo(), 
        			gcClbxd.getCreateUser(), DateUtils.dateTime(gcClbxd.getCreateAt()), 
        			gcClbxd.getStatus().toString(), null, gcClbxd.getId().toString());
    	}
    	
    	return toAjax(1);
    }
    
    @Log(title = "不通过", businessType = BusinessType.UPDATE)
    @PostMapping("/zjlno")
    @ResponseBody
    public AjaxResult zjlno(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcClbxd obj = new GcClbxd();
    		obj.setId(Long.parseLong(id[i]));
    		obj.setStatus(4);
    		obj.setLdsp(getLoginName());
    		obj.setLdspAt(new Date());
    		gcClbxdService.updateGcClbxd(obj); 
    		
    		GcClbxd gcClbxd = gcClbxdService.selectGcClbxdById(Long.parseLong(id[i]));
    		//发送消息通知
        	gcSendTipService.sendTip("clbx", gcClbxd.getType(), gcClbxd.getOrderNo(), 
        			gcClbxd.getCreateUser(), DateUtils.dateTime(gcClbxd.getCreateAt()), 
        			gcClbxd.getStatus().toString(), null, gcClbxd.getId().toString());
    	}
    	return toAjax(1);
    }
    
    @GetMapping("/clbxdprint/{id}")
    public String clbxdprint(@PathVariable("id") Long id, ModelMap mmap){
    	GcClbxd obj = gcClbxdService.selectGcClbxdById(id);
        
    	if(obj!=null && obj.getAuthority()!=null){
    		SysDept dept = deptService.selectDeptById(obj.getAuthority());
    		obj.setAuthorityName(dept.getDeptName());
    	}
    	
    	GcClbxditem items = new GcClbxditem();
        items.setClbxdId(id);
        List<GcClbxditem> itemlis= gcClbxditemService.selectGcClbxditemList(items);
        mmap.put("count", itemlis.size());
        double jtje=0.0;
        double ccbzje=0.0;//出差补助金额
        double ccbzzs=0.0;//出差补助住宿
        double qtfyjesnjt=0.0;//其它费用市内交通
        double qtfyjejery=0.0;//其它费用燃油费
        double qtfyjetcf=0.0;//其它费用停车费
        double ts=0.0;//天数
        double ccbzbz=0.0;//出差补助标准
        
        for(GcClbxditem item:itemlis){
        	jtje+=Double.parseDouble(item.getJtje()==null?"0":item.getJtje().toString());
        	ccbzje+=Double.parseDouble(item.getCcbzje()==null?"0":item.getCcbzje().toString());
        	ccbzzs+=Double.parseDouble(item.getCcbzzs()==null?"0":item.getCcbzzs().toString());
        	qtfyjesnjt+=Double.parseDouble(item.getQtfyjesnjt()==null?"0":item.getQtfyjesnjt().toString());
        	qtfyjetcf+=Double.parseDouble(item.getQtfyjetcf()==null?"0":item.getQtfyjetcf().toString());
        	ts+=Double.parseDouble(item.getTs()==null?"0":item.getTs().toString());
        	ccbzbz+=Double.parseDouble(item.getCcbzbz()==null?"0":item.getCcbzbz().toString());
        	qtfyjejery+=Double.parseDouble(item.getQtfyjejery()==null?"0":item.getQtfyjejery().toString());
        }
        
        if(itemlis!=null){
        	obj.setItem(itemlis);
        }
        
        mmap.put("obj", obj);
        mmap.put("jtje", jtje);
        mmap.put("ccbzje", ccbzje);
        mmap.put("ccbzzs", ccbzzs);
        mmap.put("qtfyjesnjt", qtfyjesnjt);
        mmap.put("qtfyjejery", qtfyjejery);
        mmap.put("qtfyjetcf", qtfyjetcf);
        mmap.put("jine", jtje+ccbzje+ccbzzs+qtfyjesnjt+qtfyjejery+qtfyjetcf);
        mmap.put("ts", ts);
        mmap.put("ccbzbz", ccbzbz);
        return prefix + "/clbxdprint";
    }
    
    @GetMapping("/clbxdzjlsp/{id}")
    public String clbxdzjlsp(@PathVariable("id") Long id, ModelMap mmap){
    	GcClbxd obj = gcClbxdService.selectGcClbxdById(id);
        
    	GcClbxditem items = new GcClbxditem();
        items.setClbxdId(id);
        List<GcClbxditem> itemlis= gcClbxditemService.selectGcClbxditemList(items);
        mmap.put("count", itemlis.size());
        double jtje=0.0;
        double ccbzje=0.0;//出差补助金额
        double ccbzzs=0.0;//出差补助住宿
        double qtfyjesnjt=0.0;//其它费用市内交通
        double qtfyjejery=0.0;//其它费用燃油费
        double qtfyjetcf=0.0;//其它费用停车费
        double ts=0.0;//天数
        double ccbzbz=0.0;//出差补助标准
        
        for(GcClbxditem item:itemlis){
        	jtje+=Double.parseDouble(item.getJtje()==null?"0":item.getJtje().toString());
        	ccbzje+=Double.parseDouble(item.getCcbzje()==null?"0":item.getCcbzje().toString());
        	ccbzzs+=Double.parseDouble(item.getCcbzzs()==null?"0":item.getCcbzzs().toString());
        	qtfyjesnjt+=Double.parseDouble(item.getQtfyjesnjt()==null?"0":item.getQtfyjesnjt().toString());
        	qtfyjetcf+=Double.parseDouble(item.getQtfyjetcf()==null?"0":item.getQtfyjetcf().toString());
        	ts+=Double.parseDouble(item.getTs()==null?"0":item.getTs().toString());
        	ccbzbz+=Double.parseDouble(item.getCcbzbz()==null?"0":item.getCcbzbz().toString());
        	qtfyjejery+=Double.parseDouble(item.getQtfyjejery()==null?"0":item.getQtfyjejery().toString());
        }
        
        if(itemlis!=null){
        	obj.setItem(itemlis);
        }
        
        mmap.put("obj", obj);
        mmap.put("jtje", jtje);
        mmap.put("ccbzje", ccbzje);
        mmap.put("ccbzzs", ccbzzs);
        mmap.put("qtfyjesnjt", qtfyjesnjt);
        mmap.put("qtfyjejery", qtfyjejery);
        mmap.put("qtfyjetcf", qtfyjetcf);
        mmap.put("jine", jtje+ccbzje+ccbzzs+qtfyjesnjt+qtfyjejery+qtfyjetcf);
        mmap.put("ts", ts);
        mmap.put("ccbzbz", ccbzbz);
        return prefix + "/clbxdzjlsp";
    }
    
    @GetMapping("/clbxdcwsp/{id}")
    public String clbxdcwsp(@PathVariable("id") Long id, ModelMap mmap){
    	GcClbxd obj = gcClbxdService.selectGcClbxdById(id);
        
    	GcClbxditem items = new GcClbxditem();
        items.setClbxdId(id);
        List<GcClbxditem> itemlis= gcClbxditemService.selectGcClbxditemList(items);
        mmap.put("count", itemlis.size());
        double jtje=0.0;
        double ccbzje=0.0;//出差补助金额
        double ccbzzs=0.0;//出差补助住宿
        double qtfyjesnjt=0.0;//其它费用市内交通
        double qtfyjejery=0.0;//其它费用燃油费
        double qtfyjetcf=0.0;//其它费用停车费
        double ts=0.0;//天数
        double ccbzbz=0.0;//出差补助标准
        
        for(GcClbxditem item:itemlis){
        	jtje+=Double.parseDouble(item.getJtje()==null?"0":item.getJtje().toString());
        	ccbzje+=Double.parseDouble(item.getCcbzje()==null?"0":item.getCcbzje().toString());
        	ccbzzs+=Double.parseDouble(item.getCcbzzs()==null?"0":item.getCcbzzs().toString());
        	qtfyjesnjt+=Double.parseDouble(item.getQtfyjesnjt()==null?"0":item.getQtfyjesnjt().toString());
        	qtfyjetcf+=Double.parseDouble(item.getQtfyjetcf()==null?"0":item.getQtfyjetcf().toString());
        	ts+=Double.parseDouble(item.getTs()==null?"0":item.getTs().toString());
        	ccbzbz+=Double.parseDouble(item.getCcbzbz()==null?"0":item.getCcbzbz().toString());
        	qtfyjejery+=Double.parseDouble(item.getQtfyjejery()==null?"0":item.getQtfyjejery().toString());
        }
        
        if(itemlis!=null){
        	obj.setItem(itemlis);
        }
        
        mmap.put("obj", obj);
        mmap.put("jtje", jtje);
        mmap.put("ccbzje", ccbzje);
        mmap.put("ccbzzs", ccbzzs);
        mmap.put("qtfyjesnjt", qtfyjesnjt);
        mmap.put("qtfyjejery", qtfyjejery);
        mmap.put("qtfyjetcf", qtfyjetcf);
        mmap.put("jine", jtje+ccbzje+ccbzzs+qtfyjesnjt+qtfyjejery+qtfyjetcf);
        mmap.put("ts", ts);
        mmap.put("ccbzbz", ccbzbz);
        return prefix + "/clbxdcwsp";
    }
    
    @GetMapping("/p4clbxd/{id}")
    public String p4clbxd(ModelMap mmap,@PathVariable("id") Long id){
    	GcProject po = gcProjectService.selectGcProjectById(id);
    	mmap.put("gcProject", po);
    	
    	//判断是成员，还是管理员
        int flag = 0;//成员
        SjProjectPeople sjProjectPeople = new SjProjectPeople();
        sjProjectPeople.setDeleteFlag(1);
        sjProjectPeople.setGcProjectid(id);
        sjProjectPeople.setCode(getUserId().toString());
        List<SjProjectPeople> peoplelis = sjProjectPeopleService.selectSjProjectPeopleList(sjProjectPeople);
        if(peoplelis!=null && peoplelis.size()>0){
        	for(SjProjectPeople po1:peoplelis){
            	if(po1.getpId()==0l){
            		flag = 1;//领导
            		break;
            	}
            }
        }
        mmap.put("flag", flag);
        return "sj/project/projectDetailClbxd";
    }

    /**
     * 查询费用核销列表
     */
    @PostMapping("/p4list")
    @ResponseBody
    public TableDataInfo p4list(GcClbxd gcClbxd){
        gcClbxd.setDeleteFlag(1);
      //角色数据权限 ==根据用户定义的数据权限查看项目数据
        List<SysRole> roles = getSysUser().getRoles();
        List<Long> roleIds = new ArrayList<Long>();
        for(SysRole obj:roles){
        	roleIds.add(obj.getRoleId());
        }
        
        int count = roleService.selectCountRoleDeptByRoleId(roleIds);
        if(count != 0){
        	//看部门
        	gcClbxd.setRoleIds(roleIds);
        }else{
        	//看个人
        	if(getSysUser().isShowRoleIds(roleIds)){
        		gcClbxd.setOwnerId(getUserId());
            }
        }
        //----
        List<GcClbxd> list = gcClbxdService.selectGcClbxdList(gcClbxd);
        return getDataTable(list);
    }
    
  //---------------------审批方法
    //申请人部门PID
    private List<SjProcessConfig> returnUser(Long deptId){
    	SysDept dept = deptService.selectDeptById(deptId);
    	SjProcessConfig sjProcessConfig = new SjProcessConfig();
        sjProcessConfig.setDeleteFlag(1);
        sjProcessConfig.setCode(processCode);
        sjProcessConfig.setDeptId(dept.getParentId());
        List<SjProcessConfig> configlis = sjProcessConfigService.selectSjProcessConfigList(sjProcessConfig);
		
        if(configlis==null || configlis.size()==0){
        	SjProcessConfig sjProcessConfig1 = new SjProcessConfig();
        	sjProcessConfig1.setDeleteFlag(1);
        	sjProcessConfig1.setCode("morenliucheng");
            List<SjProcessConfig> configlis1 = sjProcessConfigService.selectSjProcessConfigList(sjProcessConfig1);
            return configlis1;
        }
        return configlis;
    }
    
    /**
     * 修改保存项目立项
     */
    @Log(title = "差旅报销", businessType = BusinessType.UPDATE)
    @PostMapping("/edit4check")
    @ResponseBody
    public AjaxResult edit4check(GcClbxd gcClbxd){
    	try {
        	gcClbxd.setUpdateAt(new Date());
        	gcClbxd.setUpdateUser(getLoginName());
        	gcClbxdService.updateGcClbxd4Check(gcClbxd);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return toAjax(0);
		}
        return toAjax(1);
    }
    
    /**
     * level1
     * @return
     */
    @GetMapping("/level1")
    public String level1(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	GcClbxd gcClbxd = gcClbxdService.selectGcClbxdById(Long.parseLong(p.getBusinessKey().toString()));
        	GcClbxditem gcClbxditem = new GcClbxditem();
        	gcClbxditem.setClbxdId(gcClbxd.getId());
            List<GcClbxditem> items = gcClbxditemService.selectGcClbxditemList(gcClbxditem);
            gcClbxd.setItem(items);
            mmap.put("obj", gcClbxd);
        	
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
            
            //下一级审核人
            List<SjProcessConfig> configlis = returnUser(Long.valueOf(gcClbxd.getDeptId().toString()));
            mmap.put("approveid", configlis.get(0).getLevel2id());
            mmap.put("approvename", configlis.get(0).getLevel2name());
            
          //所属
            SysDept dept = new SysDept();
            dept.setDelFlag("1");
            dept.setParentId(100l);
            List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
            mmap.put("deptlis", deptlis);
        }
        
        return prefix + "/level1check";
    }
    
    /**
     * level1
     * @return
     */
    @GetMapping("/level2")
    public String level2(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	GcClbxd gcClbxd = gcClbxdService.selectGcClbxdById(Long.parseLong(p.getBusinessKey().toString()));
        	GcClbxditem gcClbxditem = new GcClbxditem();
        	gcClbxditem.setClbxdId(gcClbxd.getId());
            List<GcClbxditem> items = gcClbxditemService.selectGcClbxditemList(gcClbxditem);
            gcClbxd.setItem(items);
            mmap.put("obj", gcClbxd);
        	
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
            
          //下一级审核人
            mmap.put("approveid", gcClbxd.getHjId());
            mmap.put("approvename", gcClbxd.getHj());
            
          //所属
            SysDept dept = new SysDept();
            dept.setDelFlag("1");
            dept.setParentId(100l);
            List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
            mmap.put("deptlis", deptlis);
        }
        
        return prefix + "/level2check";
    }
    /**
     * level1
     * @return
     */
    @GetMapping("/level3")
    public String level3(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	GcClbxd gcClbxd = gcClbxdService.selectGcClbxdById(Long.parseLong(p.getBusinessKey().toString()));
        	GcClbxditem gcClbxditem = new GcClbxditem();
        	gcClbxditem.setClbxdId(gcClbxd.getId());
            List<GcClbxditem> items = gcClbxditemService.selectGcClbxditemList(gcClbxditem);
            gcClbxd.setItem(items);
            mmap.put("obj", gcClbxd);
        	
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
            
          //下一级审核人
            mmap.put("approveid", gcClbxd.getFzjl1Id());
            mmap.put("approvename", gcClbxd.getFzjl1());
            
          //所属
            SysDept dept = new SysDept();
            dept.setDelFlag("1");
            dept.setParentId(100l);
            List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
            mmap.put("deptlis", deptlis);
        }
        
        return prefix + "/level3check";
    }
    /**
     * level1
     * @return
     */
    @GetMapping("/level4")
    public String level4(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	GcClbxd gcClbxd = gcClbxdService.selectGcClbxdById(Long.parseLong(p.getBusinessKey().toString()));
        	GcClbxditem gcClbxditem = new GcClbxditem();
        	gcClbxditem.setClbxdId(gcClbxd.getId());
            List<GcClbxditem> items = gcClbxditemService.selectGcClbxditemList(gcClbxditem);
            gcClbxd.setItem(items);
            mmap.put("obj", gcClbxd);
        	
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
            
          //下一级审核人
            mmap.put("approveid", gcClbxd.getFzjl2Id());
            mmap.put("approvename", gcClbxd.getFzjl2());
            
          //所属
            SysDept dept = new SysDept();
            dept.setDelFlag("1");
            dept.setParentId(100l);
            List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
            mmap.put("deptlis", deptlis);
        }
        
        return prefix + "/level4check";
    }
    /**
     * level1
     * @return
     */
    @GetMapping("/level5")
    public String level5(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	GcClbxd gcClbxd = gcClbxdService.selectGcClbxdById(Long.parseLong(p.getBusinessKey().toString()));
        	GcClbxditem gcClbxditem = new GcClbxditem();
        	gcClbxditem.setClbxdId(gcClbxd.getId());
            List<GcClbxditem> items = gcClbxditemService.selectGcClbxditemList(gcClbxditem);
            gcClbxd.setItem(items);
            mmap.put("obj", gcClbxd);
        	
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
            
          //下一级审核人
            mmap.put("approveid", gcClbxd.getZjlId());
            mmap.put("approvename", gcClbxd.getZjl());
            
          //所属
            SysDept dept = new SysDept();
            dept.setDelFlag("1");
            dept.setParentId(100l);
            List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
            mmap.put("deptlis", deptlis);
        }
        
        return prefix + "/level5check";
    }
    
    /**
     * level1
     * @return
     */
    @GetMapping("/level6")
    public String level6(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	GcClbxd gcClbxd = gcClbxdService.selectGcClbxdById(Long.parseLong(p.getBusinessKey().toString()));
        	GcClbxditem gcClbxditem = new GcClbxditem();
        	gcClbxditem.setClbxdId(gcClbxd.getId());
            List<GcClbxditem> items = gcClbxditemService.selectGcClbxditemList(gcClbxditem);
            gcClbxd.setItem(items);
            mmap.put("obj", gcClbxd);
        	
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
            
          //下一级审核人
            mmap.put("approveid", gcClbxd.getCnId());
            mmap.put("approvename", gcClbxd.getCn());
            
          //所属
            SysDept dept = new SysDept();
            dept.setDelFlag("1");
            dept.setParentId(100l);
            List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
            mmap.put("deptlis", deptlis);
        }
        
        return prefix + "/level6check";
    }
    
    /**
     * level1
     * @return
     */
    @GetMapping("/level7")
    public String level7(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	GcClbxd gcClbxd = gcClbxdService.selectGcClbxdById(Long.parseLong(p.getBusinessKey().toString()));
        	GcClbxditem gcClbxditem = new GcClbxditem();
        	gcClbxditem.setClbxdId(gcClbxd.getId());
            List<GcClbxditem> items = gcClbxditemService.selectGcClbxditemList(gcClbxditem);
            gcClbxd.setItem(items);
            mmap.put("obj", gcClbxd);
        	
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
            
          //下一级审核人
            List<SjProcessConfig> configlis = returnUser(Long.valueOf(gcClbxd.getDeptId().toString()));
            mmap.put("approveid", configlis.get(0).getLevel8id());
            mmap.put("approvename", configlis.get(0).getLevel8name());
            
          //所属
            SysDept dept = new SysDept();
            dept.setDelFlag("1");
            dept.setParentId(100l);
            List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
            mmap.put("deptlis", deptlis);
        }
        
        return prefix + "/level7check";
    }
    
    /**
     * 修改单据
     * @param taskid
     * @param mmap
     * @return
     */
    @GetMapping("/modify")
    public String modify(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	GcClbxd gcClbxd = gcClbxdService.selectGcClbxdById(Long.parseLong(p.getBusinessKey().toString()));
        	GcClbxditem gcClbxditem = new GcClbxditem();
            gcClbxditem.setClbxdId(gcClbxd.getId());
            List<GcClbxditem> items = gcClbxditemService.selectGcClbxditemList(gcClbxditem);
            for(GcClbxditem item:items){
            	item.setCfrq(DateUtils.addDays(item.getCfrq(), 1));
            	item.setDdrq(DateUtils.addDays(item.getDdrq(), 1));
            }
            gcClbxd.setItem(items);
            mmap.put("obj", gcClbxd);
        	
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
            
          //所属
            SysDept dept = new SysDept();
            dept.setDelFlag("1");
            dept.setParentId(100l);
            List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
            mmap.put("deptlis", deptlis);
        }
        
        return prefix + "/modifycheck";
    }
}